package com.agritech.chain;

public abstract class AbstractSafetyHandler implements SafetyHandler {
    protected SafetyHandler nextHandler;
    protected String handlerName;

    public AbstractSafetyHandler(String handlerName) {
        this.handlerName = handlerName;
    }

    @Override
    public void setNextHandler(SafetyHandler nextHandler) {
        this.nextHandler = nextHandler;
    }

    // 模板方法，子类实现具体的处理逻辑
    protected abstract boolean canHandle(String requestType);
    protected abstract void processRequest(String details);

    @Override
    public void handleRequest(String requestType, String details) {
        if (canHandle(requestType)) {
            System.out.println("[" + handlerName + "] 正在处理请求类型: " + requestType + ", 详情: " + details);
            processRequest(details);
        } else if (nextHandler != null) {
            System.out.println("[" + handlerName + "] 无法处理请求类型: " + requestType + ", 传递给下一个处理器。");
            nextHandler.handleRequest(requestType, details);
        } else {
            System.out.println("所有处理器都无法处理请求类型: " + requestType + ", 详情: " + details + ". 事件未处理。");
        }
    }
}